package zdream.pmfield.test.stage.effect;

import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

import zdream.pmfield.core.Result;
import zdream.pmfield.data.foe.BStat;
import zdream.pmfield.data.foe.Foe;
import zdream.pmfield.data.foe.MoveTemplate;
import zdream.pmfield.stage.StageBuilderForTest;
import zdream.pmfield.stage.StageWrapper;
import zdream.pmfield.stage.message.ActionRequestMessage;
import zdream.pmfield.stage.message.FoeAddBuffMessage;
import zdream.pmfield.stage.message.FoeRemoveBuffMessage;
import zdream.pmfield.stage.message.RollReportMessage;
import zdream.pmfield.stage.message.TurnFinishedMessage;
import zdream.pmfield.stage.message.TurnStartMessage;
import zdream.pmfield.storage.FoeTemplateStorage;
import zdream.pmfield.test.utils.ResourceConstant;
import zdream.pmfield.test.utils.StageConstant;

import static org.junit.Assert.*;

/**
 * @author Zdream
 * @date 2023-12-07
 * @since 0.0.1
 */
public class BoundBuffCase {
	FoeTemplateStorage storage;

	@Before
	public void before() {
		storage = new FoeTemplateStorage();
		storage.load();
		storage.loadMovesInResource(ResourceConstant.RES_PATH_TEST_MOVES_JSON);
	}

	@Test
	public void testBoundBuff() {
		MoveTemplate boundMove = storage.findMoveById(10000026, 1);
		assertEquals("肯定束缚", boundMove.name);
		MoveTemplate normalMove = storage.findMoveById(10000015, 1);
		assertEquals("无效果", normalMove.name);

		Foe foe1 = Foe.builder(storage.findFoeById(4))
				.withLevel(30)
				.withMove(boundMove)
				.skipShuffleVStat()
				.build();
		Foe foe2 = Foe.builder(storage.findFoeById(1))
				.withLevel(30)
				.withMove(normalMove)
				.skipShuffleVStat()
				.build();
		assertTrue(foe1.bStats[BStat.SP.ordinal()] > foe2.bStats[BStat.SP.ordinal()]);

		final StageWrapper wrapper = StageBuilderForTest.builder()
				.putFoeToOneSide(foe1)
				.putFoeToAnotherSide(foe2)
				.withStorage(storage)
				.disableDistanceCheck()
				.buildWrapper();

		System.out.println("TEST 1: apply bound");
		AtomicInteger checked = new AtomicInteger(0);

		wrapper.printBeforeHandleMessage();
		wrapper.onDefaultActionRequest();
		wrapper.runUntil(TurnStartMessage.class, message -> message.foe.foe == foe1);

		wrapper.clearOnMessageHooks();
		wrapper.onActionRequestForAll(wrapper.createDefaultController().withActionChecker(action -> {
			assertEquals(boundMove, action.move.move);
			assertEquals(0, checked.getAndIncrement());
		}));
		wrapper.onMessage(FoeAddBuffMessage.class, message -> {
			assertTrue(message.buff.is(StageConstant.EFFECT_BOUND));
			assertTrue(message.success);
			assertEquals(1, checked.getAndIncrement());
		});
		wrapper.onMessage(TurnFinishedMessage.class, message -> {
			assertEquals(2, checked.getAndIncrement());
		});

		wrapper.runUntil(stage0 -> checked.get() == 3);
		assertEquals(3, checked.get());
		float applyTime = wrapper.now();

		System.out.println("TEST 2: bound effect: 被束缚之后 move point 不是零, 但是移动会失败");
		checked.set(0);
		wrapper.runUntil(TurnStartMessage.class, message -> message.foe.foe == foe2);

		wrapper.clearOnMessageHooks();
		wrapper.runUntil(RollReportMessage.class, message -> {
			assertEquals(StageConstant.ROLL_TITLE_MOVE_POINT, message.title);
			return true;
		});
		wrapper.runUntil(ActionRequestMessage.class, message -> {
			assertFalse(message.movable);
			assertEquals(foe2, message.foe.foe);
			assertTrue(message.foe.actionPoint > 0);

			// 不能移动
			final Result result = message.createReplyTemplate()
					.withMove(message.foe.findMoveToMove())
					.withTarget(message.foe.seat.left)
					.commit();
			assertFalse(result.success);
			assertNotNull(result.errorMessage);
			assertTrue(result.errorMessage.contains("无法移动"));
			return true;
		});
		wrapper.onDefaultActionRequest();

		wrapper.onMessage(FoeRemoveBuffMessage.class, message -> {
			throw new RuntimeException("照理说一回合内是不会自动恢复的, 肯定 foe2 的 turn 先触发");
		});
		wrapper.runUntil(TurnFinishedMessage.class);

		System.out.println("TEST 3: bound 效果会自动恢复");
		assertTrue(wrapper.now() - applyTime <= 1);

		wrapper.clearOnMessageHooks();
		wrapper.onDefaultActionRequest();
		wrapper.runUntil(FoeRemoveBuffMessage.class, message -> {
			assertEquals(foe2, message.foe.foe);
			assertTrue(message.buff.is(StageConstant.BUFF_BOUND));
			return true;
		});

	}
}
